package org.jackysoft.struts2.workflow;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jackysoft.entity.Identifiable;
import org.jackysoft.entity.WorkflowSetting;
import org.jackysoft.opensymphony.workflow.Workflow;
import org.jackysoft.opensymphony.workflow.entity.WorkflowDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowName;
import org.jackysoft.service.SystemServiceSettingService;
import org.jackysoft.service.VariableService;
import org.jackysoft.service.VariableSettingService;
import org.jackysoft.service.WorkflowNameService;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.service.WorkflowSettingService;
import org.jackysoft.struts2.ServletAction;
import org.jackysoft.workflow.ActionItem;
import org.jackysoft.workflow.StepOwner;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
import com.opensymphony.xwork2.interceptor.annotations.BeforeResult;

@Results(
		{
			@Result(
				name="toproceed",
				type = "chain", 
				params = {
						"actionName", "proceed", 
						"namespace","/workflow/proceed", 
						"method","proceed",
						"auto","true"
						//"skipActions ","inputStream", 
				
				}
			)
		}
)
public abstract class AbstractWorkflowAction<R extends Identifiable> 
extends ServletAction<R> implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6626931369693628818L;
	protected final static String COLLECT = "collect";
	protected final static String CURRENT = "current";
	protected final static String HISTORY = "history";	
	protected VariableSettingService variableSettingService;
	protected SystemServiceSettingService sysServiceSettingService;
	protected WorkflowSettingService entitySettingService;
	protected WorkflowService workflowService;
	protected VariableService variableService;
	protected WorkflowNameService nameService;
	protected Workflow workflow;
	protected WorkflowName workflowName;
	protected String workflowPk;
	protected String processName;
	protected WorkflowDescriptor workflowDescriptor;
	
	protected String[] workflowNames;
	protected int actionId=-1;
	protected long workflowId;
	protected int stepId=-1;
	protected String actionName;
	protected ActionItem actionItem;
	protected WorkflowSetting setting;
	protected Collection<StepOwner> nextOwners;
	
	public void setWorkflowId(long workflowId) {
		this.workflowId = workflowId;
	}

	public long getWorkflowId() {
		return workflowId;
	}

	public WorkflowName getWorkflowName() {
		return workflowName;
	}

	
	@Resource(name = "basicWorkflow")
	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}

	public String[] getWorkflowNames() {
		return workflowNames;
	}

	public void setWorkflowNames(String[] workflowNames) {
		this.workflowNames = workflowNames;
	}

	public int getActionId() {
		return actionId;
	}

	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

    
	


	
	@Before
	public void beforeAction() throws Exception{
		workflow.configContext();
		log.debug("before workflow Action");
	}
	

	
	@After
	public void afterAction() throws Exception{
		log.debug("after workflow Action");
	}
	
	@BeforeResult
	public void beforeResult(){
		log.info("before result return back");
	}

	public WorkflowService getWorkflowService() {
		return workflowService;
	}

	@Resource
	public void setWorkflowService(WorkflowService workflowService) {
		
		this.workflowService = workflowService;
		
	}

	public VariableService getVariableService() {
		return variableService;
	}

	@Resource
	public void setVariableService(VariableService variableService) {
		this.variableService = variableService;
	}

	@Resource
	public void setNameService(WorkflowNameService nameService) {
		this.nameService = nameService;
	}

	
	
	public void setWorkflowName(WorkflowName workflowName) {
		this.workflowName = workflowName;
	}

	public ActionItem getActionItem() {
		return actionItem;
	}

	public void setActionItem(ActionItem actionItem) {
		this.actionItem = actionItem;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	
	public WorkflowSetting getSetting() {
		return setting;
	}


	public void setSetting(WorkflowSetting setting) {
		this.setting = setting;
	}

	
	public WorkflowSettingService getEntitySettingService() {
		return entitySettingService;
	}


	@Resource
	public void setEntitySettingService(WorkflowSettingService entitySettingService) {
		this.entitySettingService = entitySettingService;
	}

	@Resource
	public void setVariableSettingService(
			VariableSettingService variableSettingService) {
		this.variableSettingService = variableSettingService;
	}

	@Resource
	public void setSysServiceSettingService(
			SystemServiceSettingService sysServiceSettingService) {
		this.sysServiceSettingService = sysServiceSettingService;
	}

	public WorkflowDescriptor getWorkflowDescriptor() {
		return workflowDescriptor;
	}

	public void setWorkflowDescriptor(WorkflowDescriptor workflowDescriptor) {
		this.workflowDescriptor = workflowDescriptor;
	}

	public String getWorkflowPk() {
		return workflowPk;
	}

	public void setWorkflowPk(String workflowPk) {
		this.workflowPk = workflowPk;
	}

	public Collection<StepOwner> getNextOwners() {
		return nextOwners;
	}

	public void setNextOwners(Collection<StepOwner> nextOwners) {
		this.nextOwners = nextOwners;
	}

	public int getStepId() {
		return stepId;
	}

	public void setStepId(int stepId) {
		this.stepId = stepId;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}

	public String getProcessName() {
		return processName;
	}

	public void setProcessName(String processName) {
		this.processName = processName;
	}	
	
}
